<template>
	<scroll-view class="form-container bg" :scroll-y="true">
        <blueList @val="getBlueVal"></blueList>
        <view v-if="activeTab === 0" class="margin-top-10">
            <jkFormCard title="基本信息" :show-right-icon="true">
                <u-icon slot="right" name="scan" size="24" @click="onScanBtn"></u-icon>
                <jk-form-item label="工序" :show-right-icon="false">
                    <view>{{ ruleForm.categoryName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="机台" :show-right-icon="false">
                    <view>{{ ruleForm.categoryName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="品种" :show-right-icon="false">
                    <view>{{ ruleForm.productName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="批号" :show-right-icon="false">
                    <view>{{ ruleForm.batchCode || '' }}</view>
                </jk-form-item>
            </jkFormCard>
            <view class="margin-top-30">
                <jkFormCard title="称重数据">
                    <!--<u-icon slot="right" name="scan" size="24" @click="onScanBtn"></u-icon>-->
                    <view slot="right">{{ count }}</view>
                    <input v-model="realTimeVal" type="digit" @input="onIptChange" border style="border: solid 1rpx #eee;border-radius: 8rpx;height: 84rpx;padding: 0 24rpx;" @blur="onBlur">
                </jkFormCard>
            </view>
        </view>
        <view v-if="activeTab === 1" class="margin-top-30">
            <jkFormCard title="包装信息">
                <jk-form-item label="车间" :show-right-icon="false">
                    <view>{{ ruleForm.workshopName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="日期" :show-right-icon="false">
                    <view>{{ ruleForm.belongDate || '' }}</view>
                </jk-form-item>
                <jk-form-item label="成分" :show-right-icon="false">
                    <view>{{ ruleForm.ingredient || '' }}</view>
                </jk-form-item>
                <jk-form-item label="净重" :show-right-icon="false">
                    <view>{{ ruleForm.suttle || '' }}</view>
                </jk-form-item>
                <jk-form-item label="纸管" :show-right-icon="false">
                    <view>{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="外袋" :show-right-icon="false">
                    <view>{{ ruleForm.outerBagList ? ruleForm.outerBagList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="内袋" :show-right-icon="false">
                    <view>{{ ruleForm.innerBagList ? ruleForm.innerBagList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="扎线" :show-right-icon="false">
                    <view>{{ ruleForm.bundleList ? ruleForm.bundleList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="备注" label-position="top" :show-border-bottom="false">
                   <textarea
                       disabled
                       placeholder-class="color4"
                       class="disabled-textarea-style"
                       :cursor-spacing="100"
                       :adjust-position="true"
                       v-model="ruleForm.remark"
                       placeholder=""
                   ></textarea>
                </jk-form-item>
            </jkFormCard>
        </view>
		<view class="fixed-footer">
			<button @click="onConfirmBtn" type="primary" :loading="confirmLoading" style="width: 100%;" size="large">确定</button>
		</view>
		<u-datetime-picker
            v-model="selectDateTime"
            :show="selectDateTimePickerState"
            mode="datetime"
			:closeOnClickOverlay="true"
            @confirm="onConfirmBtnOfSelectPicker"
            @close="onCloseBtnOfSelectPicker"
			@cancel="onCloseBtnOfSelectPicker"
        ></u-datetime-picker>
        <jkPopupOption
            :show="selectWarehousePopupOptionState"
            :optionList="warehouseList"
            @visible-change="(e) => selectWarehousePopupOptionState = e"
            @confirm="onConfirmOfSelectWarehouse"
        ></jkPopupOption>
	</scroll-view>
</template>

<script>
    import {
        productionPackagePlanGet,
        yongYouInventoryProductStockList, yongYouWarehouseList
    } from "../../../api";
    import {
        formatTime,
        showLoading
    } from "../../../utils/jk-utils";
        import jkPopup from '../../../jk-components/jk-popup';
    import jkFormCard from "../../../jk-components/jk-from-card";
    import jkFormItem from "../../../jk-components/jk-from-item";
    import jkPopupOption from "../../../jk-components/jk-popup-option";
    import blueList from './blue-list';
    let _this;
	export default {
		components: {
            blueList,
            jkPopupOption,
            jkFormCard,
            jkFormItem,
			jkPopup
		},
		data() {
			return {
                count: 0,
                realTimeVal: '',
                firstFocus: true,
                selectWarehousePopupOptionState: false,
                warehouseList: [],
				selectDateTime: '',
				selectDateTimePickerState: false,
				confirmLoading: false,
				activeTab: 0,
				tabList: ['基本信息', '包装信息'],
				dataId: null,
				ruleForm: {
                    "batchCode": "",
                    "batchId": 0,
                    "beforeNumber": 0,
                    "bundleList": [],
                    "empCode": "",
                    "empId": 0,
                    "empName": "",
                    "innerBagList": [],
                    "invalidDateTime": "",
                    "number": 0,
                    "outerBagList": [],
                    "packingPlanCode": "",
                    "packingPlanId": 0,
                    "paperTubeList": [],
                    "productCode": "",
                    "productId": 0,
                    "productName": "",
                    "productionDateTime": "",
                    "remark": "",
                    "suttle": 0,
                    "type": 0,
                    "warehouseCode": "",
                    "warehouseId": 0,
                    "warehouseName": "",
                    "workshopCode": "",
                    "workshopId": 0,
                    "workshopName": ""
				},
                operationType: '1'
			}
		},
		onLoad(e) {
            this.operationType =  e.type;
            this.dataId = e.id;
		},
		onShow() {
            _this = this;
			this.getDependentData();
		},
		methods: {
		    getBlueVal(v) {
                console.log('得到', v)
                // 修正this，否则指向会有问题
                _this.realTimeVal = v;
                wx.showToast({
                    title: `称重数据：${_this.realTimeVal}`,
                    icon: 'success'
                })
            },
            onBlur() {
                // this.firstFocus = false;
                // console.log('失去焦点')
            },
            onIptChange(e) {
                console.log('监听：', e.detail.value, typeof e.detail.value, Number(e.detail.value))
            },
            onClearWarehouse() {
                this.$set(this.ruleForm, 'warehouseId', '');
                this.$set(this.ruleForm, 'warehouseCode', '');
                this.$set(this.ruleForm, 'warehouseName', '');
            },
            onConfirmOfSelectWarehouse(e, item) {
                this.$set(this.ruleForm, 'warehouseId', item.id);
                this.$set(this.ruleForm, 'warehouseCode', item.code);
                this.$set(this.ruleForm, 'warehouseName', item.name);
                this.selectDateTimePickerState = false;
                this.getProductItemStockRequest();
            },
            onSelectTime(type) {
                this.timeType = type;
                this.selectDateTimePickerState = true;
            },
            onScanBtn() {
                let _this = this;
                // 允许从相机和相册扫码
                uni.scanCode({
                    success: function (res) {
                        let scanData = '';
                        try {
                            scanData = res.result;
                        } catch {
                            wx.showToast({
                                title: '无效的二维码',
                                icon: 'error'
                            })
                        }
                        if (scanData) {
                            wx.showModal({
                                title: '扫码结果',
                                content: `识别到计划:${scanData}`,
                                success(res) {
                                    if (res.confirm) {
                                        _this.getDetailRequest(scanData);
                                    }
                                }
                            })
                        } else {
                            wx.showToast({
                                title: '二维码格式不符',
                                icon: 'error'
                            })
                        }
                    }
                });

            },
            getDetailRequest(code) {
                return productionPackagePlanGet({ code }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                        this.$set(this.ruleForm, 'type', this.operationType);
                        this.$set(this.ruleForm, 'warehouseId', '');
                        this.$set(this.ruleForm, 'warehouseCode', '');
                        this.$set(this.ruleForm, 'warehouseName', '');
                        this.$set(this.ruleForm, 'beforeNumber', '');
                        this.$set(this.ruleForm, 'productionDateTime', formatTime(Date.now()));
                        this.$set(this.ruleForm, 'invalidDateTime', formatTime(Date.now()));
                        this.$set(this.ruleForm, 'packingPlanId', res.data.res.id);
                        this.$set(this.ruleForm, 'packingPlanCode', res.data.res.code);
                        delete this.ruleForm.id;
                        this.getProductItemStockRequest();
                        if (!this.ruleForm) {
                            wx.showToast({
                                title: '无效的二维码',
                                icon: 'error'
                            })
                        }
                    }
                });
            },
            onTransferNumber(e, type) {
                this.$set(this.ruleForm, type, Number(e));
            },
		    onDeleteTime() {
                this.$set(this.ruleForm, 'startSystemTime', null);
            },
            onOutputRatioBlur(e) {
                if (e > 1) {
                    this.$set(this.ruleForm, 'outputRatio', 1);
                }
            },
			onUpdateTime() {
				this.selectDateTimePickerState = true;
			},
			onCloseBtnOfSelectPicker() {
				this.selectDateTimePickerState = false;
			},
			onConfirmBtnOfSelectPicker(e) {
				this.$set(this.ruleForm, this.timeType, formatTime(e.value));
				this.selectDateTimePickerState = false;
			},
			onConfirmBtn() {
                this.confirmLoading = true;
                console.log('改前',  this.realTimeVal)
                setTimeout(() => {
                    this.count++;
                    this.realTimeVal++;
                    console.log('改后',  this.realTimeVal)
                    this.firstFocus = true;
                    this.confirmLoading = false;
                }, 1000);
                /*if (this.ruleForm.warehouseId && this.ruleForm.outInNumber && this.ruleForm.packingPlanId) {
                    this.confirmLoading = true;
                    this.ruleForm.prdNoticeMachineChangeId = this.dataId;
                    let params = JSON.parse(JSON.stringify(this.ruleForm));
                    productionStockRecord(params).then(res => {
                        this.confirmLoading = false;
                        if (res.data.status === 200) {
                            uni.navigateBack({
                                delta: 1
                            });
                            tipBar('save');
                        }
                    })
                } else {
                    uni.showToast({
                        title: '请填写完成',
                        icon: 'error'
                    });
                }*/
			},
			onTabChange(e) {
				this.activeTab = e;
			},
            getWarehouseListRequest() {
                return yongYouWarehouseList().then(res => {
                    if (res.data.status === 200) {
                        this.warehouseList = res.data.res;
                    }
                });
            },
            getProductItemStockRequest() {
                if (this.ruleForm.productId && this.ruleForm.warehouseId && this.ruleForm.batchCode) {
                    return yongYouInventoryProductStockList({
                        productId:  this.ruleForm.productId,
                        warehouseId:  this.ruleForm.warehouseId,
                        batchCode:  this.ruleForm.batchCode
                    }).then(res => {
                        if (res.data.status === 200) {
                            console.log('库存', res.data.res)
                            this.$set(this.ruleForm, 'beforeNumber', res.data.res);
                        }
                    });
                }
            },
			async getDependentData() {
				showLoading(true);
				this.selectDateTime = formatTime(Date.now());
				await this.getWarehouseListRequest();
                // this.$set(this.ruleForm, 'invalidDateTime', formatTime(Date.now()));
                // this.$set(this.ruleForm, 'productionDateTime', formatTime(Date.now()));
				showLoading(false);
			}
		}
	}
</script>

<style>

</style>
<style lang="scss" scoped>
	.machine-title {
		height: 42px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #2d8cf0;
		border-radius: 12rpx;
		padding: 12rpx 24rpx;
		box-sizing: border-box;
        border: solid 1px #2d8cf0;
		text-align: center;
		font-size: 34rpx;
		font-weight: 500;
		margin: 10rpx;
	}

	.popup-content-box {
		padding: 32rpx;

		.popup-content-box-title {
			font-size: 34rpx;
			text-align: center;
			margin-bottom: 20rpx;
		}
	}
</style>
